package user;

import dao.BasicDao;
import dao.EmployeeDaoFile;
import dao.EmployeeDaoList;
import dao.EmployeeDatabaseDao;
import domain.Employee;
import utils.DateAndString;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @version 1.0
 * @autho 3505
 * @date 2018/6/2
 */
public class User {
    //此类负责输入信息形成Employee对象
    private int kind;
    private List<Employee> list = new ArrayList();
    static Scanner scanner = new Scanner(System.in);
    static Employee employee=null;
    static String name=null;
    private BasicDao dao=null;
    //视图
    public void view(){
        System.out.println("--------------员工管理系统--------------");
        System.out.println("1.添加员工");
        System.out.println("2.修改员工");
        System.out.println("3.删除员工");
        System.out.println("4.查找一个员工");
        System.out.println("5.查找所有员工");
    }
    //添加
    public void add(){
        DateAndString dateAndString = new DateAndString();
        System.out.println("请输入员工ID");
        int employeeID = Integer.valueOf(scanner.nextLine());
        System.out.println("请输入员工姓名");
        String employeeName = scanner.nextLine();
        System.out.println("请输入性别");
        String sex = scanner.nextLine();
        System.out.println("请输入出生日期");
        String birthDate = scanner.nextLine();
        Date date1 = dateAndString.stringToDate(birthDate);
        System.out.println("请输入雇佣日期");
        String hirDate = scanner.nextLine();
        Date date2 = dateAndString.stringToDate(hirDate);
        System.out.println("请输入工资");
        Float salary = Float.valueOf(scanner.nextLine());
        System.out.println("请输入部门ID");
        int departmentID = Integer.valueOf(scanner.nextLine());
        employee = new Employee(employeeID, employeeName, sex, date1, date2, salary, departmentID);
        System.out.println(dao.add(employee));
    }
    //修改
    public void modify(){
        System.out.println("请输入姓名");
        name = scanner.nextLine();
        System.out.println(dao.searchOne(name));
        System.out.println("请输入工资");
        String salary1 = scanner.nextLine();
        employee.setEmployeeName(name);
        employee.setSalary(Float.valueOf(salary1));
        System.out.println(dao.modify(employee));
    }
    //删除
    public void del(){
        System.out.println("请输入姓名");
        name = scanner.nextLine();
        System.out.println(dao.delete(name));
    }

    public User(int kind) throws Exception {
        this.kind = kind;
        if(kind==1){
            dao=new EmployeeDaoList();
        }else if(kind==2){
            dao=new EmployeeDaoFile();
        }else if(kind==3){
            dao=new EmployeeDatabaseDao();
        }

        while (true) {
            employee = new Employee();
            name = null;
            view();
            int choice = scanner.nextInt();
            //吸收回车
            scanner.nextLine();
            switch (choice) {
                case 1:
                    add();
                    break;
                case 2:
                    modify();
                    break;
                case 3:
                   del();
                    break;
                case 4:
                    System.out.println("请输入姓名");
                    name = scanner.nextLine();
                    dao.searchOne(name);
                    break;
                case 5:
                    System.out.println(dao.searchAll());
                    break;
                default:
                    System.out.println("您输入有误，请重新输入");
            }
        }

    }
    }

